<?php
/**
 * @name /www/donations/model.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.03.31
 * 
 * Functionality for building the layout of the donations page.
 */

function BuildTable(&$donations, $isAdmin) {
  $db = new DB();
  // Use ($('#__A').css('display') == 'none')
  // to determine if the user is an admin or not;
  // the element will show if the user is indeed an admin
?><span id="__A" type="Hidden" class="Admin" style="position: absolute; top: 0px; left: -9999px; width: 1px; height: 1px; font-size: 1px;"></span>
<div style="width: 800px; margin-left: auto; margin-right: auto;">
<table class="display" id="Donations" cellpadding="5" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>View</th>
      <th>Rec.</th>
      <th>Sent</th>
      <th>Progress</th>
      <th>Donor</th>
      <th>Lines</th>
      <th>Qty. Rec.</th>
      <th>Qty. Sent</th>
    </tr>
  </thead>
  <tbody>
<?
  foreach ($donations as $donation) {
    $lastDateReceived = $donation->GetLastDateReceived();
    $received = $donation->GetDonatedItemsReceivedCount();
    $pledged = $donation->GetDonatedItemsPledgedCount();
?>
    <tr>
      <td><a class="ViewDonationButton"></a><input type="hidden" class="DonationId" value="<?=htmlspecialchars($donation->DonationId) ?>" /></td>
      <td><?=$lastDateReceived ? date('m/d/y', $lastDateReceived) : '' ?></td>
      <td><?=date('m/d/y', $donation->DateDonated) ?></td>
      <td><?=!$pledged ? 'n/a' : '<div class="ProgressDiv" style="height: 15px;"><input type="hidden" value="' . floor($received * 100 / $pledged * 1) . '" /></div>' ?></td>
      <td><a href="../EditUser.php?uId=<?=$donation->User->UserId ?>"><?=htmlspecialchars($donation->User->FirstName . ' ' . $donation->User->LastName) ?></a></td>
      <td><?=htmlspecialchars(count($donation->DonatedItems)) ?></td>
      <td><?=htmlspecialchars($received) ?></td>
      <td><?=htmlspecialchars($pledged) ?></td>
    </tr>
<?
  }
?>
  </tbody>
</table>
<div style="margin-top: 10px;">
  <a id="AddDonationButton"></a>
</div>
</div>
<?
}

function BuildEditDialog($isAdmin) {
?><div id="DonationDialog">
  <form method="POST">
    <input type="hidden" id="Action" name="Action" />
    <input type="hidden" id="DonationId" name="DonationId" />
    <div>
      <a id="AddDonatedItemButton"></a>
      <?=$isAdmin ? '<a id="DeleteDonationButton"></a>' : '' ?>
    </div>
    <div id="DonorName" class="Admin"></div>
    <div id="DateDonated" class="Admin"></div>
    <div style="width: 850px;">
      <table id="DonatedItemsTemplate" cellspacing="0" cellpadding="5" border="1" class="display" style="width: 850px;">
        <thead>
          <tr>
            <th>Action</th>
            <th>Name</th>
            <th>Quantity Pledged</th>
            <th>Quantity Received</th>
          </tr>
        </thead>
        <tbody>
          <tr id="DonatedItemTemplate">
            <td class="Actions">
              <a class="DeleteDonatedItemButton"></a>
              <a class="EditDonatedItemButton"></a>
              <input type="hidden" class="DonatedItemId" />
              <input type="hidden" class="ItemId" />
            </td>
            <td class="Name"></td>
            <td class="QuantityPledged"></td>
            <td class="QuantityReceived"></td>
          </tr>
        </tbody>
      </table>
    </div>
    <div id="DonatedItems"></div>
  </form>
</div>
<div id="DonatedItemDialog">
  <form>
    <?
      $isw = new ItemSearchWidget();
      $isw->IsVerified = ($isAdmin ? -1 : true);
      $isw->SubmitEnabled = false;
      $isw->DisplayWidget();
    ?>
    <div>
      <input type="hidden" id="DonatedItemId" />
      Quantity: <input type="text" class="Quantity" style="width: 40px;" />
    </div>
    <div class="Admin">
      Qty. Received: <input type="text" class="QuantityReceived" style="width: 40px;" />
    </div>
  </form>
</div>
<?
}